<template>
  <div class="order-manage-container">
    <el-row class="order-manage-top-row">
      <el-col :span="6">
        <div class="order-manage-top-col" style="padding-left:0">
          <el-date-picker style="width:100%"
            v-model="time"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholde="开始时间"
            end-placeholde="结束时间"
            size="small">
          </el-date-picker>
        </div>
      </el-col>
      <el-col :span="4">
        <div class="order-manage-top-col">
          <span style="padding:0 5px;max-width:100px">佣金状态</span>
          <el-select v-model="Param.balance_status" placeholder="选择退款状态" size="small">
            <el-option label="全部" value=""></el-option>
            <el-option label="待结算" value="0"></el-option>
            <el-option label="已结算" value="1"></el-option>
            <el-option label="已拒绝" value="2"></el-option>
          </el-select>
        </div>
      </el-col>
      <el-col :span="4">
        <div class="order-manage-top-col">
          <span style="padding:0 5px">订单状态</span>
          <el-select v-model="Param.order_status" placeholder="选择退款状态" size="small" style="max-width:150px">
            <el-option :label="item.label" :value="item.key" v-for="item in orderStatus" :key="item.key"></el-option>
          </el-select>
        </div>
      </el-col> 
      <el-col :span="4">
        <div class="order-manage-top-col">
          <span style="padding:0 5px">所属团长</span>
          <el-input v-model="Param.searchName" size="small"></el-input>
        </div>
      </el-col>
      <el-col :span="4">
        <div class="order-manage-top-col">
          <span style="padding:0 5px">订单号</span>
          <el-input placeholder="请输入内容" v-model="Param.order_sn" class="input-with-select" size="small"/>
        </div>
      </el-col>
      <el-col :span="2">
        <div class="order-manage-top-col">
          <el-button type="primary" size="small" style="padding:8px 20px;" @click="search">搜 索</el-button>
        </div>
      </el-col>
    </el-row>

    <div style="margin: 20px 0">
      <el-button type="primary" size="small" style="padding:8px 20px;" @click="handleDownload">导出数据</el-button>
    </div>

    <!-- 表格 -->
    <div style="margin-top: 15px">
      <el-table :data="recoreList" style="width:100%" stripe>
        <el-table-column prop="id" label="ID" align="center"></el-table-column>
        <el-table-column label="头像" align="center">
          <template slot-scope="scope">
            <l-pic :picurl="scope.row.avatar" :size="{width:40,height:40}" :disdel="false" :disview="false"/>
          </template>
        </el-table-column>        
        <el-table-column prop="realname" label="会员信息" align="center"></el-table-column>
        <el-table-column prop="phone" label="手机号码" align="center"></el-table-column>
        <el-table-column prop="content" label="佣金类型" align="center"></el-table-column>
        <el-table-column prop="remain_money" label="所得佣金" align="center"></el-table-column>
        <el-table-column prop="status" label="状态" align="center"></el-table-column>
        <el-table-column prop="confirm_time" label="结算时间" align="center"></el-table-column>
        <el-table-column label="当前订单状态" align="center">
          <template slot-scope="scope">
            <dir>{{ orderStatus.find(item => item.key === scope.row.order_status).label }}</dir>
          </template>
        </el-table-column> 
      </el-table>
    </div>

      <!-- 分页 -->
    <div style="text-align:right;margin:15px 15px">
      <el-pagination class="page"
        background
        layout="total, prev, pager, next, jumper"
        :total="count"
        :page-size="10"
        :current-page="page"
        @size-change="changePage"
        @current-change="changePage">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getRecordList } from '@/api/finance'
import LPic from '@/components/lPic'
import { parseTime } from '@/utils/index'

export default {
  name: 'finance-record',
  components: {
    LPic
  },
  data() {
    return {
      page: 1,
      count: 1,
      recoreList: [],

      Param: {
        balance_status: '',
        order_status: '',
        searchName: '',
        order_sn: '',
        type: '2',
        key: this.$store.state.app.activeApp.saa_key,
        page: 1,
        limit: 10,
        datetime: ''
      },
      time: '',
      pickerOptions: {
        shortcuts: [{
          text: '近7天',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start,end]);
          }
        },
        {
          text: '近30天',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start,end]);
          }
        }]
      },
      orderStatus: [
        { label: '所有订单', key: '' },
        { label: '待发货', key: '0' },
        { label: '待团长收货', key: '1' },
        { label: '待用户收货', key: '2' },
        { label: '已退款', key: '3' },
        { label: '已完成', key: '4' },
      ],
    }
  },
  mounted() {
    this.getRecordList();
  },
  watch: {
    time(newTime) {
      if(newTime!==null){
        const start_time = parseTime(newTime[0],'{y}-{m}-{d} {h}:{i}:{s}');
        const end_time = parseTime(newTime[1],'{y}-{m}-{d} {h}:{i}:{s}');  
        this.Param.datetime = start_time + ' - ' + end_time;
      }else{
        this.Param.datetime = '';
      }        
    }
  },
  methods: {
    /**
     * 获取流水列表
     */
    getRecordList() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        type: '2',
        page: this.page,
        limit: 10
      };
      getRecordList(params).then(res => {
        if(res.status === 200){
          this.recoreList = res.data;
          this.count = parseInt(res.count);
        }else if(res.status === 204){
          this.recoreList = [];
          this.count = 1;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 搜索
     */
    search() {
      getRecordList(this.Param).then(res => {
        if(res.status === 200){
          this.recoreList = res.data;
          this.count = parseInt(res.count);
        }else if(res.status === 204){
          this.recoreList = [];
          this.count = 1;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 导出
     */
    handleDownload() {
      import('@/utils/Export2Excel').then(excel => {
        const tHeader = ['ID', '姓名', '佣金类型', '金额', '状态', '结算时间']
        const filterVal = ['id', 'realname', 'content', 'remain_money', 'status', 'confirm_time']
        const list = this.recoreList
        const data = this.formatJson(filterVal, list)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: '佣金流水',
          autoWidth: true,
          bookType: 'xlsx'
        })
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
          return v[j]
      }))
    },

    /**
     * 翻页
     */
    changePage(page) {
      this.page = page;
      this.getRecordList();
    },
  },
}
</script>

<style lang="scss" scoped>
.order-manage-container{
  min-height: calc(100vh - 100px);
  margin: 20px;
  background-color: #fff;
  padding:25px;
  box-shadow: 0px 0px 4px 0px rgba(115, 115, 115, 0.2);
} 
.order-manage-top-row{
  height: 32px;
  line-height: 32px;
  text-align: center;
}
.order-manage-top-col{
  padding: 0 15px;
  max-width: 95%;
  overflow: hidden;
  display: flex;
  &>span{
    flex: 2;
    display: inline-block;
    padding: 0 5px;
    white-space: nowrap; 
    overflow: hidden;
  }
  &>div{
    flex: 4
  }
}
</style>